// cd D:\北工大\B503\合作论文\注册制和金融效率\code
* step1
clear
import excel "D:\北工大\项目\注册制改革与企业融资效率\psm_data\psm_data_2019.xlsx", sheet("Sheet1") firstrow

gen policy = Time - 2020
tab policy
replace policy = -3 if policy < -3
replace policy =3 if policy > 3


* 生成年份虚拟变量与实验组虚拟变量的交互项  
forvalues i = 3(-1)1 {  
    gen pre_`i' = (policy == -`i' & treat == 1)  
}  
gen current = (policy == 0 & treat == 1)  
forvalues j = 1(1)3 {  
    gen post_`j' = (policy == `j' & treat == 1)  
}  
  

* step2 
  
* 运行reghdfe命令（这里只运行一个作为示例）  
* 注意：确保weight_in_kg, male, height, study_online, x1, x2, x3, x4, x5, x6 ,x7等变量都已正确定义  
* 省份 行业级别 Time   * 省份 行业级别 * 省份 Time *省份 省份 行业级别 Time   
 
//  reghdfe y pre_3 pre_2 pre_1 current post_1 post_2 post_3 x1 x2 x3 x4 x5  x6, absorb(Time)  

//  reghdfe y pre_3 pre_2 pre_1 current post_1 post_2 post_3 x1 x2 x3 x4 x5 x6  , absorb(省份)  

//  reghdfe y pre_3 pre_2 pre_1 current post_1 post_2 post_3 x1 x2 x3 x4 x5 x6, absorb(行业级别) 

//  reghdfe y pre_3 pre_2 pre_1 current post_1 post_2 post_3 x1 x2 x3 x4 x5 x6 , absorb(省份 Time)  

//  reghdfe y pre_3 pre_2 pre_1 current post_1 post_2 post_3 x1 x2 x3 x4 x5 x6, absorb(行业级别 Time)  

//  reghdfe y pre_3 pre_2 pre_1 current post_1 post_2 post_3 x1 x2 x3 x4 x5 x6 , absorb(省份 行业级别)  

 reghdfe y pre_3 pre_2 pre_1 current post_1 post_2 post_3 x1 x2 x3 x4 x5 x6, absorb(省份 行业级别 Time)  
 
 

* 画图
coefplot, baselevels vertical keep(pre_3 pre_2  pre_1 current post_1 post_2 post_3) ///  
    omitted order(pre_3 pre_2 pre_1 current post_1 post_2 post_3) ///  
    level(95) yline(0, lcolor(edkblue*0.8)) ///  
    xline(3, lwidth(vthin) lpattern(dash) lcolor(teal)) ///  
    ylabel(, labsize(*0.75)) xlabel(, labsize(*0.75)) ///  
    ytitle("注册制动态效应", size(small)) ///  
    xtitle("注册制时点", size(small)) ///  
    addplot(line @b @at) ///  
    ciopts(lpattern(dash) recast(rcap) msize(medium)) ///  
    msymbol(circle_hollow) scheme(s1mono)
	
// outreg2 using "parallel_trend_test.doc", replace bdec(3)  tdec(3) word dec(3) title("平行趋势检验的DID回归结果")  



//将省份转变为可回归的
encode 省份 ,gen(prov)
//行业也是提取第一个字母作为行业分类
tab 行业级别
gen industrial=substr(行业级别,1,1)
tab industrial
encode industrial ,gen(industry)
//reg y i.treat##i.D x1 x2 x3 x4 x5 x6  
//创建交乘项
gen DID=treat*D
xtset id Time 
//进行did回归。可以在x5 后面加入其他控制变量，以及加上i.id或者别的进行控制
xtreg y DID x1 x2 x3 x4 x5 x6 , re

//安慰剂检验
reghdfe y DID  x1 x2 x3 x4 x5 x6, absorb(省份 行业级别 Time) vce(robust)
cap erase "simulations.dta"
permute DID beta = _b[DID] se = _se[DID] df = e(df_r), reps(500) rseed(123) saving("simulations.dta"): reghdfe y DID  x1 x2 x3 x4 x5 x6, absorb(省份 行业级别 Time) vce(robust)
use "simulations.dta", clear
gen t_value = beta / se
gen p_value = 2 * ttail(df, abs(beta/se))
dpplot beta, xline(0.4040, lc(black*0.5) lp(dash)) xline(0, lc(black*0.5) lp(solid))xtitle("Estimator", size(*0.8)) ytitle("Density", size(*0.8))
dpplot t_value, xtitle("T-value", size(*0.8)) ytitle("Density", size(*0.8))
twoway (scatter p_value beta)(kdensity beta,yaxis(2)),xline(0.0085) yline(0.1,lpattern(dash))

//中介效应检验
// SA显著 
reg y treat x1 x2 x3 x4 x5 x6    //分析 x 和 y 之间的关系
reg SC treat x1 x2 x3 x4 x5 x6    //分析 x 和 m 之间的关系
reg y SC treat  x1 x2 x3 x4 x5 x6   // 加入 m，看 x 和 y 之间的关系

// net install sgmediation2, from("https://tdmize.github.io/data/sgmediation2")
// drop if missing(y,treat,Z,x1,x2,x3,x4,x5,x6 )
// sgmediation2 y iv(i.treat) mv(Z) cv(x1 x2 x3 x4 x5 x6)